package com.artPreter.utils;

import java.util.ArrayList;
import java.util.List;

public class PageUtils {

    /**
     * 获取页码列表
     * @param totalPage
     * @param currentPage
     * @param showPageCount
     * @return
     */
    public static List<Number> getPageList(Integer totalPage, Integer currentPage,Integer showPageCount) {
        if(currentPage<1){
            currentPage = 1;
        }
        List<Number> pageList = new ArrayList<>();
        if(totalPage-currentPage<showPageCount){
            for (int i = totalPage-showPageCount ; i < totalPage; i++) {
                if(i+1>0){

                    if(pageList.isEmpty()){
                        if(i-1>0){
                            pageList.add(i-1);
                        }
                        if(i>0){
                            pageList.add(i);
                        }
                    }
                    pageList.add(i+1);
                }

            }
        }else{
            for (int i = 0; i < showPageCount; i++) {
                if(i+currentPage<=totalPage){
                    if(pageList.isEmpty()){
                        if(currentPage-2>0){
                            System.out.println(currentPage-2);
                            pageList.add(currentPage-2);
                        }
                        if(currentPage-1>0){
                            System.out.println(currentPage-1);
                            pageList.add(currentPage-1);
                        }
                    }
                    pageList.add(i+currentPage);
                }
            }
        }



        return pageList;
    }

    /**
     * 只保留中间三个
     * @param pageList
     * @param max
     * @param currentPage
     * @return
     */
    public static List<Number> collectPageList3(List<Number> pageList,Integer max,Integer currentPage) {
        if(max<3||currentPage==1){
            return pageList;
        }
        if(currentPage<max){
            List<Number> newList = new ArrayList<>();
            newList.add(currentPage-1);
            newList.add(currentPage);
            newList.add(currentPage+1);
            return newList;
        }
        if(currentPage.equals(max)){
            List<Number> newList = new ArrayList<>();
            if(max>3){
                newList.add(currentPage-3);
            }
            newList.add(currentPage-2);
            newList.add(currentPage-1);
            newList.add(currentPage);
            return newList;
        }
        return pageList;

    }
}
